//
//  LXWelcomeViewController.swift
//  weibo
//
//  Created by lixin on 16/4/19.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

import SDWebImage

class LXWelcomeViewController: UIViewController {
    
    override func loadView() {
        
        view = backgroundImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        setHeadViewImageViewAnimate()
    }
    
    //MARK: - 更新headImageView约束
    private func setHeadViewImageViewAnimate(){
    
        self.headImageView.snp_updateConstraints { (make) in
            
            make.top.equalTo(self.view).offset(100)
        }
        
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            
                self.view.layoutIfNeeded()//刷新布局
            
            }) { (_) in
                
                UIView.animateWithDuration(0.3, animations: { 
                    
                        self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName("chooseNav", object: "welcome")
                })
        }
    
    }
    
    //MARK: - 设置视图
    private func setUI(){
    
        view.addSubview(headImageView)
        
        view.addSubview(welcomeLabel)
        
        headImageView.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 85, height: 85))
            
            make.centerX.equalTo(view)
            
            make.top.equalTo(view).offset(400)
        }
        
        welcomeLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(headImageView)
            
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }

    //MARK: - 懒加载控件
    
    //背景图片
    private lazy var backgroundImageView:UIImageView = UIImageView(image:UIImage(named: "ad_background"))
    
    //用户头像
    private lazy var headImageView:UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 85/2
        
        imageView.clipsToBounds = true
        
        imageView.layer.borderColor = ThemeColor.CGColor
        
        imageView.layer.borderWidth = 2
        
        imageView.sd_setImageWithURL(NSURL(string:LXUserAccountTools.sharedTools.userAccount?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        
        return imageView
        
    }()
    
    //欢迎回来label
    private lazy var welcomeLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.darkGrayColor(), textFont: 17)
        
        label.textAlignment = .Center
        
        label.text = "欢迎回来"
        
        label.alpha = 0
        
        return label
        
    }()
    
}
